
package com.sso.core.ssoauth.utils;

import cn.hutool.core.util.StrUtil;

import com.sso.core.ssoauth.domain.LoginAppUser;
import lombok.experimental.UtilityClass;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import sun.security.util.SecurityConstants;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 安全工具类
 *
 * @author 库琦
 */
@UtilityClass
public class SecurityUtils {

	/**
	 * 获取Authentication
	 */
	public Authentication getAuthentication() {
		return SecurityContextHolder.getContext().getAuthentication();
	}

	/**
	 * 获取用户
	 * @param authentication
	 * @return LoginAppUser
	 * <p>
	 */
	public LoginAppUser getUser(Authentication authentication) {
		Object principal = authentication.getPrincipal();
		if (principal instanceof LoginAppUser) {
			return (LoginAppUser) principal;
		}
		return null;
	}

	/**
	 * 获取用户
	 */
	public LoginAppUser getUser() {
		Authentication authentication = getAuthentication();
		return getUser(authentication);
	}


}
